package com.aaa.service.impl;

import com.aaa.entity.Product;
import com.aaa.mapper.ProductMapper;
import com.aaa.service.ProductService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService{
    @Autowired
    private ProductMapper productMapper;
    @Override
    @SentinelResource(value = "saveOrder" , blockHandler = "getShopInfoBlockHandler" , fallback = "getShopInfoFallback")
    public Product getShopInfo(Long pid) {
        if(pid == 1){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        Product product = productMapper.selectById(pid);
        return product;
    }

    public String getShopInfoBlockHandler(String id , BlockException e){
        return "先溜了";
    }

    public String getShopInfoFallback(String id , Throwable throwable){
        return "熔断了";
    }
}
